//
//  ChallengeViewController.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/29.
//

#import "ChallengeViewController.h"
#import "CommonUtil.h"
#import "LeftTitleView.h"
#import "ChallengeModel.h"
#import "ChallengeCell.h"
#import "RecordModel.h"
#import "RecordTableViewCell.h"
#import "ChallengeAlert.h"
@interface ChallengeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UISegmentedControl *segmentedControl;
@property (strong, nonatomic) LeftTitleView * leftTitleView;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) NSMutableArray * models;
@property (strong, nonatomic) NSMutableArray * recordModels;
@property (assign, nonatomic) NSInteger currentIndex;
@end

@implementation ChallengeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =  RGB(243.0, 243.0, 243.0);
    UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [titleBtn setTitle:@"港彩名人堂" forState:UIControlStateNormal];
    [titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    titleBtn.titleLabel.font = [UIFont systemFontOfSize:AD(14)];
    titleBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -AD(195));
    //titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, AD(5));
    [titleBtn setImage:[UIImage imageNamed:@"icon_xiala"] forState:UIControlStateNormal];
    self.navigationItem.titleView = titleBtn;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"说明" forState:UIControlStateNormal];
    [btn setTitleColor:RGB(255.0, 94.0, 122.0) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    [self.view addSubview:self.segmentedControl];
    [self.view addSubview:self.leftTitleView];
    [self.view addSubview:self.tableView];
    [self testData];
    [self testData2];
}

// 假数据
- (void)testData{
    
    self.models = [NSMutableArray array];
    for (NSInteger i = 0; i < 10; i++) {
        ChallengeModel *model = [[ChallengeModel alloc] init];
        if (i == 1 || i == 5) {
            model.moreBtn = YES;
        }
        [self.models addObject:model];
    }
    [self.tableView reloadData];
    

}

- (void)testData2{
    self.recordModels = [NSMutableArray array];
    for (NSInteger i = 0; i < 10; i++) {
        RecordModel *model = [[RecordModel alloc] init];
        [self.recordModels addObject:model];
    }
    [self.tableView reloadData];
}

- (void)titleBtnClick:(UIButton *)sender{
    NSLog(@"港彩名人堂");
}
- (void)rightBtnClick:(UIButton *)sender{
    NSLog(@"说明");
}

-(void)changeIndex:(UISegmentedControl *)sender{
    self.currentIndex =sender.selectedSegmentIndex;
    if (self.currentIndex == 0) {
        self.leftTitleView.hidden = false;
        self.tableView.frame = CGRectMake(self.leftTitleView.right+AD(3), self.segmentedControl.bottom, SCREEN_WIDTH-AD(6)-self.leftTitleView.width, self.leftTitleView.height);
    }else{
        self.leftTitleView.hidden = YES;
        self.tableView.frame = CGRectMake(0, self.segmentedControl.bottom, SCREEN_WIDTH, self.leftTitleView.height);
    }
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{\
    if (self.currentIndex == 0) {
        return self.models.count;
    }else{
        return self.recordModels.count;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.currentIndex == 0) {
        ChallengeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (self.models.count > indexPath.row) {
            cell.model = self.models[indexPath.row];
        }
        __weak typeof(self) weakSelf = self;
        cell.block = ^(NSString * _Nonnull title) {
            NSLog(@"%@",title);
            if ([title isEqualToString:@"参加"]) {
                ChallengeAlert *alert = (ChallengeAlert *)[HudFactory creatHudWithType:@"ChallengeAlert"];
                alert.actionBlock = ^(id data) {
                    NSLog(@"%@",data);
                };
                [alert show];
            }else if ([title isEqualToString:@"放弃"]){
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定放弃该任务吗？放弃后该任务将终止，且报名费不会退回！但您可以重新挑战该任务" preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *conform = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                        NSLog(@"点击了确认按钮");
                    }];
                    //2.2 取消按钮
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    NSLog(@"点击了取消按钮");
                }];
             
                //3.将动作按钮 添加到控制器中
                [alert addAction:conform];
                [alert addAction:cancel];
                
                //4.显示弹框
                [weakSelf presentViewController:alert animated:YES completion:nil];

            }
        };
        return cell;
    }else{
        RecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        __weak typeof(self) weakSelf = self;
        cell.block = ^(NSString * _Nonnull title) {
            if ([title isEqualToString:@"挑战"]) {
                ChallengeAlert *alert = (ChallengeAlert *)[HudFactory creatHudWithType:@"ChallengeAlert"];
                alert.actionBlock = ^(id data) {
                    NSLog(@"%@",data);
                };
                [alert show];
            }
        };
        if (self.recordModels.count > indexPath.row) {
            cell.model = self.recordModels[indexPath.row];
        }
        return cell;
    }

}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.currentIndex == 0) {
        return AD(200);
    }else{
        return AD(270);
    }
    
}



#pragma mark - lazy

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.leftTitleView.right+AD(3), self.segmentedControl.bottom, SCREEN_WIDTH-AD(6)-self.leftTitleView.width, self.leftTitleView.height)];
        [_tableView registerClass:[ChallengeCell class] forCellReuseIdentifier:@"cell"];
        [_tableView registerClass:[RecordTableViewCell class] forCellReuseIdentifier:@"cell2"];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = false;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (UISegmentedControl *)segmentedControl{
    if (_segmentedControl==nil) {
        _segmentedControl=[[UISegmentedControl alloc]initWithItems:@[@"我的挑战",@"领奖记录"]];
        _segmentedControl.tintColor=[UIColor whiteColor];
        _segmentedControl.selectedSegmentTintColor = RGB(255.0, 90.0, 117.0);
        _segmentedControl.selectedSegmentIndex=0;
        [_segmentedControl addTarget:self
                              action:@selector(changeIndex:)
                    forControlEvents:UIControlEventValueChanged];
        _segmentedControl.frame = CGRectMake(0, kNavBarAndStatusBarHeight+AD(3), SCREEN_WIDTH, AD(35));
    }
    return _segmentedControl;
}


- (LeftTitleView *)leftTitleView{
    if (!_leftTitleView) {
        CGFloat h = SCREEN_HEIGHT  - self.segmentedControl.height;
        _leftTitleView = [[LeftTitleView alloc] initWithFrame:CGRectMake(0, self.segmentedControl.bottom, AD(70), h)];
    }
    return _leftTitleView;
}


@end
